import Vue from 'vue'
import { login, getInfo, logout } from '@/api/login'
import { getCurrentNodeInfo } from '@/api/node'
import { ACCESS_TOKEN } from '@/store/mutation-types'

const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {},
    currentNode: {}, // 当前节点
    virtualNode: {} // 虚节点
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_MENUS: (state, menus) => {
      state.menus = menus
    },
    SET_ROUTES: (state, routes) => {
      state.addRoutes = routes
    },
    SET_CURRENT_NODE: (state, routes) => {
      state.currentNode = routes
    },
    SET_VIRTUAL_NODE: (state, routes) => {
      state.virtualNode = routes
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      // eslint-disable-next-line no-undef
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          const { data } = response
          Vue.ls.set(ACCESS_TOKEN, data.token, 7 * 24 * 60 * 60 * 1000)
          Vue.ls.remove('error_100001')
          commit('SET_TOKEN', data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      // eslint-disable-next-line no-undef
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(response => {
          const { data } = response
          if (!data) {
            // eslint-disable-next-line prefer-promise-reject-errors
            reject('Verification failed, please Login again.')
          }

          const { permissions, menus, realName, id } = data
          // roles must be a non-empty array
          if (!permissions || permissions.length <= 0) {
            // eslint-disable-next-line prefer-promise-reject-errors
            reject('getInfo: roles must be a non-null array!')
          }

          commit('SET_ID', id)
          commit('SET_ROLES', permissions)
          commit('SET_MENUS', menus)
          commit('SET_NAME', realName)
          // commit('SET_AVATAR', avatar)
          // commit('SET_INTRODUCTION', introduction)
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    },


    // 查询节点信息
    GetCurrentNode({ commit, state }) {
      // eslint-disable-next-line no-undef
      return new Promise((resolve, reject) => {
        getCurrentNodeInfo(state.token).then(response => {
          const { data } = response
          commit('SET_CURRENT_NODE', data)
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout({ commit, state }) {
      // eslint-disable-next-line no-undef
      return new Promise((resolve) => {
        logout(state.token).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        }).finally(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_MENUS', [])
          commit('SET_CURRENT_NODE', {})
          Vue.ls.remove(ACCESS_TOKEN)
        })
      })
    }

  }
}

export default user
